package com.jm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.reggie.common.R;
import com.jm.reggie.dto.SetmealDto;
import com.jm.reggie.entity.Category;
import com.jm.reggie.entity.Dish;
import com.jm.reggie.entity.Setmeal;
import com.jm.reggie.service.CategoryService;
import com.jm.reggie.service.SetmealDishService;
import com.jm.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Resource
    private SetmealService setmealService;
    @Resource
    private SetmealDishService setmealDishService;
    @Resource
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     * @return
     */
    @GetMapping("page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage=new Page<>();

        //条件构造器
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
        //查询条件
        wrapper.like(name!=null,Setmeal::getName,name);
        //排序条件
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,wrapper);

        //对象拷贝（第一次，拷贝分页条件等)
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<SetmealDto> setmealDtoList=new ArrayList<>();
        for (Setmeal setmeal : pageInfo.getRecords()) {
            SetmealDto setmealDto=new SetmealDto();
            //对象拷贝（第二次，拷贝对象数据）
            BeanUtils.copyProperties(setmeal,setmealDto);
            //封装套餐名称
            Category category = categoryService.getById(setmeal.getCategoryId());
            if(category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            //将setmealDto添加到list集合中
            setmealDtoList.add(setmealDto);
        }

        //封装records
        dtoPage.setRecords(setmealDtoList);
        return R.success(dtoPage);
    }

    /**
     * 批量修改套餐状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        //批量查找
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        setmealList.forEach(setmeal -> setmeal.setStatus(status));
        //批量修改
        setmealService.updateBatchById(setmealList);
        return R.success("修改套餐状态成功");
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("批量删除套餐成功");
    }

    /**
     * 根据条件查询套餐列表
     * @param setmeal
     * @return
     */
    @GetMapping("list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        //添加条件，查询状态1(起售)的套餐
        wrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        //查询排序条件
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }
}
